package com.ruoyi.procurement.design;

import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

@Component
public class ProcurementStateHandlerFactory {

    private static final Map<ProcurementStateEnum, ProcurementStateHandler> content =
            new EnumMap<>(ProcurementStateEnum.class);

    @Autowired
    public ProcurementStateHandlerFactory(List<ProcurementStateHandler> handlers) {
        handlers.forEach(handler -> content.put(ProcurementStateEnum.fromCode(handler.get()), handler));
    }

    /**
     * 执行器
     * @param orderID 订单Id
     * @param state 订单要进入的状态
     * @return 结果
     */
    public Integer execute(Long orderID, String state) {
        ProcurementStateHandler procurementStateHandler = content.get(ProcurementStateEnum.fromCode(state));
        if (procurementStateHandler == null) {
            throw new ServiceException("未找到对应状态流转中心！");
        }
        return procurementStateHandler.handler(orderID, state);
    }
}
